package com.gbits.app.finance.convert;

import java.util.List;
import java.util.Map;

import com.gbits.app.finance.api.ItemFinanceConvert;
import com.gbits.app.finance.domain.CostOrgSubjectMapper;
import com.gbits.app.finance.domain.Subject;
import com.gbits.app.finance.domain.SubjectBusinessMapper;

public class CmpSubjectConvert extends ItemFinanceConvert {
	private boolean debit;

	public boolean isDebit() {
		return debit;
	}

	public void setDebit(boolean debit) {
		this.debit = debit;
	}
	@Override
	public String getUsageInfo(Map<String, Object> map) {
		return this.getUsage(map);
	}

	@Override
	public String getSubjectDetail(Map<String, Object> map) {
		String orgCode = this.getCompanyCode(map);
		CostOrgSubjectMapper cosm = this.getCostOrgSubjectMapperService().get(orgCode, this.getCostOrgName(map));
		
		String subjectCode = cosm.getSubjectCode();
		List<Subject> subjects = this.getSubjectService().getSubjectsDetail(subjectCode, this.getSubjectName(map), orgCode);
		Subject last = subjects.get(subjects.size() - 1);
		SubjectBusinessMapper sbm = this.getSubjectBusinessMapperService().getBusinessMapper(this.getCompanyCode(map), last.getName());
		String type = sbm.getBusinessTypeName();
		if ("与员工相关".equalsIgnoreCase(type)) {
			return this.getEmpString(map);
		} else if ("与日常经营相关".equalsIgnoreCase(type)) {
			return this.getBusinessString(map);
		}
		throw new RuntimeException("业务类型不适用");
	}
	
	protected String getEmpString(Map<String, Object> map) {
		List<Subject> list = this.getSubjectService().getSubjectsByCmpEmp(this.getCompanyCode(map));
		StringBuffer sb = new StringBuffer();
		for(Subject s : list) {
			sb.append(SUBJECT_SPLIT + s.getName());
		}
		Subject s = this.getSubjectService().getCompanyByEmp(this.getCompanyCode(map), this.getAccountName(map));
		
		String name = s.getName();
		String[] arr = name.split("]");
		sb.append("/");
		sb.append(arr[0].replace("[", ""));
		sb.append(SUBJECT_SPLIT);
		sb.append(arr[1]);
		
		String str = list.get(list.size() - 1).getCode() + sb.toString();
		return str;
	}
	protected String getBusinessString(Map<String, Object> map) {
		List<Subject> list = this.getSubjectService().getSubjectsByCmp(this.getCompanyCode(map));
		StringBuffer sb = new StringBuffer();
		for(Subject s : list) {
			sb.append(SUBJECT_SPLIT + s.getName());
		}
		Subject s = this.getSubjectService().getCompanyByBusiness(this.getCompanyCode(map), this.getAccountName(map));
		
		String name = s.getName();
		String[] arr = name.split("]");
		sb.append("/");
		sb.append(arr[0].replace("[", ""));
		sb.append(SUBJECT_SPLIT);
		sb.append(arr[1]);
		
		String str = list.get(list.size() - 1).getCode() + sb.toString();
		return str;
	}

	@Override
	public String getDebitAmount(Map<String, Object> map) {
		if (this.isDebit()) {
			return this.getAmount(map);
		}
		return "";
	}

	@Override
	public String getCreditAmount(Map<String, Object> map) {
		if (!this.isDebit()) {
			return this.getAmount(map);
		}
		return "";
	}

	@Override
	public boolean support(Map<String, Object> map) {
		return this.isCompany(map);
	}

}
